package cn.zzdt4j.springboot.starter.controller;

import cn.zzdt4j.adapter.web.WebThreadPoolHandlerChoose;
import cn.zzdt4j.adapter.web.WebThreadPoolService;
import cn.zzdt4j.common.model.Result;
import cn.zzdt4j.common.model.ThreadPoolBaseInfo;
import cn.zzdt4j.common.model.ThreadPoolParameterInfo;
import cn.zzdt4j.common.model.ThreadPoolRunStateInfo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * Web 线程池控制器。
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023/10/26 3:12
 */
@CrossOrigin
@RestController
@AllArgsConstructor
public class WebThreadPoolController {

    private final WebThreadPoolHandlerChoose webThreadPoolServiceChoose;

    @GetMapping("/web/base/info")
    public Result<ThreadPoolBaseInfo> getPoolBaseState(@RequestParam(value = "mark") String mark) {
        WebThreadPoolService webThreadPoolService = webThreadPoolServiceChoose.choose();
        if (webThreadPoolService != null && webThreadPoolService.getClass().getSimpleName().contains(mark)) {
            return new Result<ThreadPoolBaseInfo>().setCode(Result.SUCCESS_CODE).setData(webThreadPoolService.simpleInfo());
        }
        return new Result<ThreadPoolBaseInfo>().setCode(Result.SUCCESS_CODE);
    }

    @GetMapping("/web/run/state")
    public Result<ThreadPoolRunStateInfo> getPoolRunState() {
        return new Result<ThreadPoolRunStateInfo>().setCode(Result.SUCCESS_CODE).setData(webThreadPoolServiceChoose.choose().getWebRunStateInfo());
    }

    @PostMapping("/web/update/pool")
    public Result<Void> updateWebThreadPool(@RequestBody ThreadPoolParameterInfo threadPoolParameterInfo) {
        webThreadPoolServiceChoose.choose().updateWebThreadPool(threadPoolParameterInfo);
        return new Result<Void>().setCode(Result.SUCCESS_CODE);
    }
}
